<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <title>条码生成器</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-start;
      height: 100%;
      /* background-color: #f5f5f5; */
      padding-top: 20vh;
    }

    p {
      margin: 0;
    }

    #inputText {
      width: 200px;
      height: 26px;
      font-size: 16px;
      padding: 5px;
      border: 1px solid #ddd;
      border-radius: 4px;
      margin-right: 10px;
    }

    input:focus {
      border: 2px solid #007bff;
      /* 选中时边框变蓝色 */
      outline: none;
      /* 移除默认的高亮边框 */
      box-shadow: 0 0 5px rgba(81, 81, 81, 0.5);
      /* 添加外发光效果 */
    }

    input {
      transition: border 0.3s, box-shadow 0.3s;
    }


    button {
      margin-right: 10px;
      background-color: #4096ff;
      color: white;
      border: none;
      box-shadow: inset 0 2px 0 rgba(5, 145, 255, 0.1);
      font-size: 16px;
      height: 32px;
      padding: 4px 15px;
      border-radius: 6px;
    }

    button:hover {
      cursor: pointer;
    }

    button:disabled {
      background-color: #ccc;
      /* 灰色背景 */
      color: #6c6c6c;
      /* 文字颜色变浅 */
      cursor: not-allowed;
      /* 鼠标指针变为禁用样式 */
      opacity: 0.6;
      /* 透明度降低 */
      border: 1px solid #bbb;
      /* 边框颜色变浅 */
      box-shadow: inset 0 2px 0px rgba(0, 0, 0, 0.2);
    }



    #container {
      display: flex;
      box-sizing: border-box;
      gap: 30px;
      margin-top: 20px;
      width: 1176px;
      height: 240px;
    }

    #barcode-wrap {
      text-align: center;
      margin-top: 32px;
      width: 841px;
      margin-left: 22px;
    }

    #textDisplay {
      display: block;
      text-align: center;
      margin-top: 0px;
      font-weight: 800;
      -webkit-font-weight: 800;
      -moz-font-weight: 800;
    }

    #qr-code {
      margin-top: 40px;
      vertical-align: top;
      padding-top: 10px;
      margin-left: 120px;
    }

    .textDisplay_box {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      /* padding-left: 24px; */
      padding-left: 16px;
    }

    .symbol {
      margin-top: 8px;
    }
  </style>
</head>

<body>
  <h1>条码生成器</h1>
  <div>
    <input type="text" id="inputText" placeholder="请输入内容">
    <button onclick="generateCodes()">生成</button>
    <button onclick="downloadImage()" id="downloadBtn" disabled>下载</button>
  </div>

  <div id="container" style="display: none;">
    <div id="barcode-wrap">
      <div><svg id="barcode"></svg></div>
      <div class="textDisplay_box">
        <p id="textDisplay"></p>
      </div>

    </div>
    <div id="qr-code"></div>
  </div>

  <!-- 引入第三方库 -->
  <script src="./js/JsBarcode.all.min.js"></script>
  <script src="./js/qrcode.min.js"></script>
  <script src="./js/html2canvas.min.js"></script>

  <script>
    function generateCodes() {
      const input = document.getElementById('inputText');
      const text = input.value.trim();
      if (!text) {
        alert("请输入内容后再生成条形码和二维码！");
        return;
      }

      // 生成条形码
      JsBarcode('#barcode', text, {
        format: 'CODE128',
        displayValue: false,
        width: 6.2,
        height: 96,
        background: "transparent"

      });

      requestAnimationFrame(() => {
        const barcodeSvg = document.getElementById('barcode');
        const bbox = barcodeSvg.getBBox(); // 获取条形码宽度
        const textDisplay = document.getElementById('textDisplay');

        textDisplay.style.width = `${bbox.width}px`;
        textDisplay.style.textAlign = 'center';
        textDisplay.style.fontSize = '44px';
        textDisplay.style.letterSpacing = '19px';
        textDisplay.style.fontWeight = '600';
        textDisplay.style.marginTop = '0px';
        textDisplay.style.display = 'flex';
        textDisplay.style.justifyContent = 'center';


      });


      // 显示文本
      // document.getElementById('textDisplay').textContent = `*${text}*`;
      document.getElementById('textDisplay').innerHTML = `
        <span class="symbol">*</span>
        <span id="testtext">${text}</span>
        <span class="symbol">*</span>
      `;

      // 生成二维码
      const qr = qrcode(0, 'L');
      qr.addData(text);
      qr.make();
      let qrSvg = qr.createSvgTag({
        cellSize: 5.5,
        margin: 1,

      });

      qrSvg = qrSvg.replace(/fill="white"/g, 'fill="none"'); // 设置背景色

      document.getElementById('qr-code').innerHTML = qrSvg;

      // 显示容器
      document.getElementById('container').style.display = 'flex';
      document.getElementById('downloadBtn').disabled = false;
    }

    function downloadImage() {
      const container = document.getElementById('container');
      // 获取文本框内容
      const inputText = document.getElementById('inputText').value.trim();


      // 获取容器的实际宽高
      const width = container.offsetWidth;
      const height = container.offsetHeight;

      // 不同浏览器在下载时，样式可能不同，例如360浏览器的字体在下载时需要手动注入样式
      const style = document.createElement('style');
      style.innerHTML = `
        #textDisplay {
          font-family: Arial, sans-serif;
        }
      `;
      container.appendChild(style);


      // 避免空值
      if (!inputText) return;

      // 复制 SVG 转换为 Canvas 以便截图
      const svgElement = document.getElementById('qr-code').firstElementChild;
      const xml = new XMLSerializer().serializeToString(svgElement);
      const svg64 = btoa(xml);
      const imgSrc = `data:image/svg+xml;base64,${svg64}`;
      const img = new Image();
      img.src = imgSrc;

      img.onload = () => {
        html2canvas(container, {
          backgroundColor: 'transparent',// 下载时背景设为透明
          scale: 1,  // 设置缩放，防止模糊
          width: width,  // 确保宽度正确
          height: height // 确保高度正确
        }).then(canvas => {
           container.removeChild(style); // 移除注入的样式
          const link = document.createElement('a');
          link.href = canvas.toDataURL('image/png');
          link.download = inputText + '.png';
          link.click();
        });
      };

      // console.log("12112width", width);
      // console.log("12112height", height);

      // setTimeout(() => {
      //   html2canvas(container, {
      //     backgroundColor: 'transparent',
      //     scale: 1,  // 设置缩放，防止模糊
      //     width: width,  // 确保宽度正确
      //     height: height // 确保高度正确
      //   }).then(canvas => {
      //     const link = document.createElement('a');
      //     link.href = canvas.toDataURL('image/png');
      //     link.download = `${inputText}.png`;
      //     link.click();
      //   });
      // }, 500);

    }
  </script>
</body>

</html>